<template>
  <div class="login">
    <!-- 头部 -->

    <!-- 主体 -->
    <el-form status-icon ref="user" label-width="100px" class="demo-user">
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model="username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="password"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="login(username, password)"
          >登录</el-button
        >
      </el-form-item>
    </el-form>

    <el-button type="primary" @click="testFn()">测试登录</el-button>
  </div>
</template>

<script>
import { codeLogin } from "@/api/login";

// import request from '@/utils/request'

export default {
  name: "LoginIndex",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    //登录
    async login(username, password) {
      //校验
      if (username.length < 4) {
        this.$message.error("用户名必须大于等于4位", false);
        console.log("用户名必须大于等于4位");
        return;
      }
      if (password.length < 6) {
        this.$message.error("密码必须大于等于6位", false);
        console.log("密码必须大于等于6位");
        return;
      }

      //请求登录
      
        const result = await codeLogin(username, password)
        console.log(result)
        if (result.code === 1) {
         
          this.$message.success("登录成功！")
          //跳转
          this.$router.push("/page");
        }else{
          this.$message.error("登录失败，请检查用户名和密码")

        }

        
      
      
      
      

    },

    async testFn() {
      try {
        const response = await this.$axios.post(
          "http://localhost:8848/api/login",
          {
            username: "sunyan",
            password: "123456",
          }
        );
        console.log(response);
      } catch (error) {
        console.log(error);
        this.$message.error("登录失败，请检查用户名和密码");
      }
    },
  },
};
</script>

<style></style>